<template>
  <div class="main_box">
    <el-row class="header-row">
      <el-col>
        <div class="search_wrap">
          <el-form :inline="true" :model="filters" class="demo-form-inline" ref="searchForm">
            <el-form-item prop="combineCode">
              <el-input v-model="filters.combineCode" placeholder="产品组合编号" clearable></el-input>
            </el-form-item>
            <el-form-item prop="combineName">
              <el-input v-model="filters.combineName" placeholder="产品组合名称" clearable></el-input>
            </el-form-item>
            <el-form-item prop="combineType">
              <el-select v-model="filters.combineType" placeholder="产品组合类型" clearable>
                <el-option v-for="item in combineTypeList" :value="item.value" :label="item.label" :key="item.value"> </el-option>
              </el-select>
            </el-form-item>
            <el-form-item prop="alarmType">
              <el-select v-model="filters.alarmType" placeholder="报警类型" clearable>
                <el-option v-for="item in alarmTypeList" :value="item.value" :label="item.label" :key="item.value"> </el-option>
              </el-select>
            </el-form-item>
            <el-form-item prop="villageId">
              <el-select v-model="filters.villageId" placeholder="请选择小区" clearable filterable>
                <el-option v-for="item in villageList" :value="item.id" :label="item.name" :key="item.id"> </el-option>
              </el-select>
            </el-form-item>
            <!-- <el-form-item prop="safetyType">
              <el-select v-model="filters.safetyType" placeholder="安全设备类型" clearable>
                <el-option v-for="item in safetyTypeList" :value="item.value" :label="item.label" :key="item.value">
                </el-option>
              </el-select>
            </el-form-item> -->
            <el-form-item>
              <el-button icon="el-icon-search" type="primary" @click="onSearch">查询</el-button>
              <el-button icon="el-icon-refresh-left" @click="resetForm">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
      <el-col>
        <div class="opration_wrap">
          <el-tooltip content="刷新" placement="top">
            <el-button circle icon="el-icon-refresh-right" @click="reload" size="mini"></el-button>
          </el-tooltip>
          <div>
            <el-button type="primary" plain icon="fa fa-plus" v-has="permissionAdd" @click="handleClickAdd()" size="mini"> 新增 </el-button>
            <el-button type="success" plain icon="el-icon-sell" v-has="permissionAdd" @click="handleBatchUpdateSaleState(2)" size="mini">上架 </el-button>
            <el-button type="warning" plain icon="el-icon-sold-out" v-has="permissionAdd" @click="handleBatchUpdateSaleState(3)" size="mini">下架 </el-button>
          </div>
        </div>
      </el-col>
    </el-row>

    <el-row ref="tableRow">
      <el-table
        ref="tableList"
        :data="dataList"
        :fit="true"
        highlight-current-row
        v-loading="listLoading"
        @row-click="$refs.tableList.toggleRowSelection($event)"
        @selection-change="handleSelectionChange"
        element-loading-text="数据加载中，请稍后..."
        empty-text="对不起，没有找到相关数据"
      >
        <el-table-column type="selection" width="50" align="center"></el-table-column>
        <el-table-column type="index" label="序号" width="50" align="center"></el-table-column>
        <el-table-column prop="combineCode" align="center" width="120" label="产品组合编号"></el-table-column>
        <el-table-column align="center" label="产品组合图片" width="160">
          <template slot-scope="scope">
            <el-image title="点击可预览" :src="scope.row.mainImg" fit="cover" :preview-src-list="[scope.row.mainImg]" :z-index="scope.$index"> </el-image>
          </template>
        </el-table-column>
        <el-table-column prop="combineName" align="center" width="240" label="产品组合名称">
          <template slot-scope="scope">
            <div>
              {{ scope.row.combineName }}
              <el-tag size="mini" effect="dark" v-if="scope.row.defaultFlag"> 默认 </el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column align="center" width="120" label="产品组合类型">
          <template slot-scope="scope">
            <div>
              <el-tag size="mini" effect="dark" :type="formatCombineTypes(scope.row.combineType)">
                {{ formatCombineType(scope.row.combineType) }}
              </el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="alarmType" align="center" width="120" label="报警类型">
          <template slot-scope="scope">
            <span>{{ $Filters.formatAlarmType(scope.row.alarmType) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="villageName" align="center" width="180" label="所属小区"></el-table-column>
        <!--<el-table-column prop="combineDesc" align="center" width="180" label="描述"></el-table-column>-->
        <el-table-column align="center" width="120" label="在售状态">
          <template slot-scope="scope">
            <div>
              <el-tag size="mini" effect="dark" :type="formatSaleStates(scope.row.saleState)">
                {{ formatSaleState(scope.row.saleState) }}
              </el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column align="center" width="120" label="创建人" prop="createUserName"> </el-table-column>
        <el-table-column align="center" width="90" label="创建时间" prop="createDate"> </el-table-column>
        <el-table-column label="状态" align="center" width="100">
          <template slot-scope="scope">
            <span>
              <el-tag size="mini" effect="dark" :type="formatStates(scope.row.state)">
                {{ formatState(scope.row.state) }}
              </el-tag>
            </span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" fixed="right" width="160">
          <template slot-scope="scope">
            <el-button v-has="permissionCheck" @click="handleClickDetail(scope.row)" size="mini">查看</el-button>
            <el-dropdown trigger="click" size="small">
              <el-button type="primary"> <i class="fa fa-list-ul"></i>更多 </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item v-has="permissionEdit" @click.native="handleClickEditor(scope.row)">编辑 </el-dropdown-item>
                <el-dropdown-item v-has="permissionUpStatus" @click.native="handleIsDisable(scope.row)">
                  {{ scope.row.state === 0 ? '启用' : '禁用' }}
                </el-dropdown-item>
                <el-dropdown-item v-has="permissionDelete" @click.native="handleClickDelete(scope.row)">删除 </el-dropdown-item>
                <el-dropdown-item v-has="permissionCopy" @click.native="handleClickCopy(scope.row)">复制 </el-dropdown-item>

              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNo"
        :page-sizes="[20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, jumper, next, pager, prev"
        :total="page.pageTotal"
      ></el-pagination>
    </el-row>
  </div>
</template>

<script>
import * as message from '@/constants/message';
import CombineApi from '@/api/goods/combine';
import VillageApi from '@/api/address/village';
import { EAlarmTypeList } from '@/enum';
export default {
  name: 'CombineManagement',
  inject: ['reload'], //注入局部刷新
  data() {
    return {
      dataList: [],
      listLoading: true,
      villageList: [],
      filters: {
        villageId: '',
        combineName: '',
        combineCode: '',
        combineType: '',
        // safetyType: ""
        alarmType: '',
      },
      combineTypeList: [
        { value: 1, label: '常规' },
        { value: 2, label: '安全报警' },
      ],
      alarmTypeList: EAlarmTypeList,
      safetyTypeList: [
        { value: 3, label: '烟雾报警器' },
        { value: 4, label: '燃气报警器' },
        { value: 5, label: '门磁报警器' },
        { value: 6, label: '水浸卫士' },
        { value: 8, label: 'SOS紧急呼叫' },
        { value: 9, label: '跌倒报警器' },
        { value: 10, label: '红外探测器' },
        { value: 15, label: '拐杖' },
        { value: 16, label: '胸牌' },
        { value: 17, label: '手表' },
      ],
      selectedForm: {
        ids: [],
        state: '',
      },
      page: {
        pageNo: 1,
        pageTotal: 0,
        pageSize: 20,
      },
      // 权限

      permissionAdd: 'SYSTEM_MANAGER_GOODS_COMBINE_LIST_ADD',
      permissionCheck: 'SYSTEM_MANAGER_GOODS_COMBINE_LIST_SEE',
      permissionEdit: 'SYSTEM_MANAGER_GOODS_COMBINE_LIST_EDIT',
      permissionDelete: 'SYSTEM_MANAGER_GOODS_COMBINE_LIST_DELETE',
      permissionUpStatus: 'SYSTEM_MANAGER_GOODS_COMBINE_LIST_STATE',
      permissionCopy:'SYSTEM_MANAGER_GOODS_COMBINE_LIST_COPY'
    };
  },
  mounted() {
    this.init();
    this.getVillageList();
    window.addEventListener('keydown', this.keyDown);
  },
  activated() {
    this.listData();
  },
  destroyed() {
    window.removeEventListener('keydown', this.keyDown, false);
  },
  components: {},
  methods: {
    init() {
      let _this = this;
      _this.listLoading = true;
      _this.listData();
      // _this.getTableHeight();
    },
    getVillageList() {
      VillageApi.villageList({}).then((res) => {
        this.villageList = res.data;
      });
    },
    keyDown(e) {
      //如果是回车则执行查询方法
      if (e.keyCode == 13) {
        this.onSearch();
      }
    },

    handleSelectionChange(value) {
      this.selectedForm.ids = value.map((item) => item.combineId);
    },
    handleSizeChange(size) {
      let _this = this;
      _this.page.pageSize = size;
      _this.listData();
    },
    handleCurrentChange(val) {
      let _this = this;
      _this.page.pageNo = val;
      _this.listData();
    },
    handleClickCopy(row){
      let _this = this;
      _this.$router.push({
        path: '/goods/combineEdit',
        query: { id: row.combineId,isCopy:true },
      });
    },
    resetForm() {
      this.$refs.searchForm.resetFields();
      this.onSearch();
    },
    onSearch() {
      this.listLoading = true;
      this.page.pageNo = 1;
      this.listData();
    },
    listData() {
      let _this = this;
      var cond = {
        pageNo: _this.page.pageNo,
        pageSize: _this.page.pageSize,
        safetyType: _this.filters.safetyType,
        combineName: _this.filters.combineName,
        combineCode: _this.filters.combineCode,
        combineType: _this.filters.combineType,
        alarmType: _this.filters.alarmType,
        villageId: _this.filters.villageId,
      };
      // 发送请求
      CombineApi.listCombine(cond)
        .then((res) => {
          _this.listLoading = false;
          _this.dataList = res.data.list;
          _this.page.pageTotal = res.data.total;
          _this.selectedForm.ids = [];
        })
        .catch((err) => {
          _this.listLoading = false;
        });
    },
    formatCombineType(cellValue) {
      if (cellValue === 1) {
        return '常规';
      } else if (cellValue === 2) {
        return '安全报警';
      }
    },
    formatCombineTypes(cellValue) {
      if (cellValue === 1) {
        return '';
      } else if (cellValue === 2) {
        return 'danger';
      }
    },
    //在售状态（1.待上架 2.上架 3.下架 4.当日售罄）
    formatSaleState(cellValue) {
      if (cellValue === 1) {
        return '待上架';
      } else if (cellValue === 2) {
        return '上架';
      } else if (cellValue === 3) {
        return '下架';
      } else if (cellValue === 4) {
        return '当日售罄';
      }
    },
    formatSaleStates(cellValue) {
      if (cellValue === 1) {
        return '';
      } else if (cellValue === 2) {
        return 'success';
      } else if (cellValue === 3) {
        return 'info';
      } else if (cellValue === 4) {
        return 'danger';
      }
    },
    formatState(cellValue) {
      if (cellValue === -1) {
        return '删除';
      } else if (cellValue === 0) {
        return '禁用';
      } else if (cellValue === 1) {
        return '启用';
      }
    },
    formatStates(cellValue) {
      if (cellValue === -1) {
        return 'danger';
      } else if (cellValue === 0) {
        return 'info';
      } else if (cellValue === 1) {
        return 'success';
      }
    },
    handleIsDisable(row) {
      let currentState = row.state === 0 ? 1 : 0;
      this.$confirm(currentState ? '是否启用？' : '是否禁用', message.TITLE_PROMPT, {}).then(() => {
        CombineApi.deleteCombine({
          id: row.combineId,
          state: currentState,
        }).then((res) => {
          this.$Utils.notifySuccess();
          this.listData();
        });
      });
    },
    handleClickDelete(val) {
      // 删除
      this.$confirm('是否删除该产品组合', message.TITLE_PROMPT, {}).then(() => {
        var cond = {
          id: val.combineId,
          state: -1,
        };
        CombineApi.deleteCombine(cond).then((data) => {
          this.$Utils.notifySuccess();
          this.listData();
        });
      });
    },
    handleClickDetail(row) {
      let _this = this;
      _this.$router.push({
        path: '/goods/combineDetail',
        query: { id: row.combineId },
      });
    },
    handleClickEditor(val) {
      //编辑 进入新增页通过id来查找数据
      let _this = this;
      _this.$router.push({
        path: '/goods/combineEdit',
        query: { id: val.combineId },
      });
    },
    handleClickAdd() {
      // 刷新
      let _this = this;
      _this.$router.push('/goods/combineAdd');
    },
    handleBatchUpdateSaleState(state) {
      this.selectedForm.state = state;
      if (this.selectedForm.ids.length == 0) {
        this.$message.error('请至少选择一项');
        return;
      }
      let message = state == 2 ? '确认上架吗？' : '确认下架吗？';
      this.$confirm(message, '提示', {}).then(() => {
        CombineApi.batchUpdateSaleState(this.selectedForm).then((data) => {
          this.$Utils.notifySuccess();
          this.listData();
        });
      });
    },
  },
};
</script>

<style lang="scss">
@import '@/assets/css/views/content.scss';
</style>
<style lang="scss" scoped>
.img-wrapper {
  width: 80px;
  height: 60px;
  overflow: hidden;
  border-radius: 3px;
  display: flex;
  justify-content: space-around;
  align-items: center;
  margin: 0 auto;

  .el-image {
    width: 100%;

    &::v-deep img {
      width: 100%;
      height: auto;
    }
  }
}
</style>
